/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package visualization;

import allparser.AllFile;
import gov.nasa.worldwind.geom.Angle;
import gov.nasa.worldwind.geom.LatLon;
import gov.nasa.worldwind.geom.Position;
import java.util.Random;

/**
 *
 * @author Dark Mark
 */
public class GroundData
{
    public int pingsNumber;
    public int beamsNumber;
    public Position[][] positions;
    public double minElevation = Double.MAX_VALUE;
    public double maxElevation = Double.MIN_VALUE;
    public double difference = 0;
    public double meanElevation = 0;
    public final int pingSkip = 16;
    public final int beamSkip = 16;
    public int currentPing = 0;

    public GroundData(AllFile file)
    {
        // wybor co ktorychs elementow
        pingsNumber = file.datagram68.size() / pingSkip;
        beamsNumber = file.datagram68.get(0).X.length / beamSkip;

        System.out.println("pingsNumber = " + pingsNumber);
        System.out.println("beamsNumber = " + beamsNumber);

        positions = new Position[pingsNumber][beamsNumber];
        double tempElevation;
        
        for (int i = 0; i < pingsNumber; i++)
        {
            for (int j = 0; j < beamsNumber; j++)
            {
                try
                {
                    // obliczenie polozenia wierzcholka na mapie
                    //positions[i][j] = new Position(new LatLon(Angle.fromDegreesLatitude(file.datagram68.get(i).pozycja.lat + file.datagram68.get(i).pozycja.speed * i / 1000), Angle.fromDegreesLongitude(file.datagram68.get(i).pozycja.lon + file.datagram68.get(i).pozycja.speed * j / 1000)), -(file.datagram68.get(i).Z[j] - 80) * 50 - 400);
                    //positions[i][j] = new Position(new LatLon(Angle.fromDegreesLatitude(file.datagram68.get(i).pozycja.lat + file.datagram68.get(i).pozycja.speed * i / 1000), Angle.fromDegreesLongitude(file.datagram68.get(i).pozycja.lon + file.datagram68.get(i).Y[j] / 10000)), file.datagram68.get(i).Z[j]);
                    positions[i][j] = new Position(new LatLon(
                            Angle.fromDegreesLatitude(file.datagram68.get(i*pingSkip).pozycja.lat),
                            Angle.fromDegreesLongitude(file.datagram68.get(i*pingSkip).pozycja.lon + (float)j/10000)),
                            (file.datagram68.get(i*pingSkip).Z[j*beamSkip]) );

                    //System.out.println(positions[i][j].latitude+", "+positions[i][j].longitude);

                    // wyszukanie najmniejszej i najwiekszej wysokosci oraz obliczenie roznicy pomiedzy nimi
                    tempElevation = positions[i][j].elevation;
                    if (tempElevation > maxElevation)
                    {
                        maxElevation = tempElevation;
                    }
                    else if (tempElevation < minElevation)
                    {
                        minElevation = tempElevation;
                    }
                }
                catch (Exception e)
                {
                    System.out.println(i);
                    break;
                }
            }
        }
        difference = Math.abs(maxElevation) - minElevation;
        meanElevation = (maxElevation - minElevation) / 2;
        //System.out.println(meanElevation);
        //System.out.println(difference);
        //System.out.println(minElevation+", "+maxElevation);
    }
}
